/* Type.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the Type class, which represents a type of data that can be
displayed by Hexie.
*/

#ifndef __MZ_TYPE_H__
#define __MZ_TYPE_H__

#include <string>

using std::string;



class Type {
public:
	// Set all types to big-endian mode.
	// The default is little-endian.
	static void SetBigEndian(bool isBigEndian);
	// Check whether Type is set to big-endian interpretation mode.
	static bool IsBigEndian();
	// Get a string representing the endian mode ("LE" or "BE").
	static string EndianString();


public:
	// Default constructor. Sets type to H1.
	Type();

	// Interpret the given location as this type. The function calling this one is
	// responsible for checking that (it + this->ElementSize() <= end of string).
	string Read(string::const_iterator it) const;

	// Set the number of bytes per element. Return false if the given length is not valid.
	bool SetElementSize(int elementSize);
	// Get the number of bytes per element.
	int ElementSize() const;

	// Set the total length. Return false if the given length is not valid.
	bool SetTotalBytes(int totalBytes);
	// Get the total length of the data.
	int TotalBytes() const;

	// Set the interpretation type. Return false if the given type is not valid.
	// H = hex, C = character, I = signed integer, U = unsigned integer, F = float.
	// Either uppercase or lowercase characters for the above are acceptable.
	bool SetDataType(char typeIdentifier);
	// Get the current data type.
	char DataType() const;

	// Get the string defining this type.
	// For example, "I4" means 4-byte signed integer.
	string TypeString() const;


private:
	// Convert the given character into a string giving its hexadecimal representation.
	static const string &HexByteString(unsigned char c);


private:
	int length;
	char block;
	char type;


private:
	static bool bigEndian;
};



#endif

/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
